import * as ftypes from './featureTypes';
import { message } from 'ant-design-vue';
import {mapManager} from './MapManager'
import {
    Pointer as PointerInteraction,
    defaults as defaultInteractions,
  } from 'ol/interaction';

  var moveTarget;
  var backCoordinate;
  var onMoved;

  function setOpts(opt){
    moveTarget = opt.moveTarget;
    backCoordinate = opt.backCoordinate;
    onMoved = opt.onMoved;
  }

class Drag extends PointerInteraction {
    constructor() {
      super({
        handleDownEvent: handleDownEvent,
        handleDragEvent: handleDragEvent,
        handleMoveEvent: handleMoveEvent,
        handleUpEvent: handleUpEvent,
      });
  
      /**
       * @type {import("../src/ol/coordinate.js").Coordinate}
       * @private
       */
      this.coordinate_ = null;
  
      /**
       * @type {string|undefined}
       * @private
       */
      this.cursor_ = 'pointer';
  
      /**
       * @type {Feature}
       * @private
       */
      this.feature_ = null;
  
      /**
       * @type {string|undefined}
       * @private
       */
      this.previousCursor_ = undefined;
    }
  }
  
  function handleDownEvent(evt) {
         var map = evt.map;
  
        var feature = map.forEachFeatureAtPixel(evt.pixel,
            function(feature) {
              return feature;
            });
        if(feature && feature.getId() != moveTarget){
            return false;
        }else if(feature){
            this.coordinate_ = evt.coordinate;
      	    backCoordinate = feature.getGeometry().clone().getCoordinates();
            this.feature_ = feature;
        }
  
        return !!feature;
  }

  function handleDragEvent(evt){
    var deltaX = evt.coordinate[0] - this.coordinate_[0];
    var deltaY = evt.coordinate[1] - this.coordinate_[1];

    var geometry = /** @type {ol.geom.SimpleGeometry} */
        (this.feature_.getGeometry());
    geometry.translate(deltaX, deltaY);

    this.coordinate_[0] = evt.coordinate[0];
    this.coordinate_[1] = evt.coordinate[1];
  }

  function handleMoveEvent(evt){
    if (this.cursor_) {
        var map = evt.map;
        var feature = map.forEachFeatureAtPixel(evt.pixel,
            function(feature) {
              return feature;
            });
        var element = evt.map.getTargetElement();
        if (feature) {
          if (element.style.cursor != this.cursor_) {
            this.previousCursor_ = element.style.cursor;
            element.style.cursor = this.cursor_;
          }
        } else if (this.previousCursor_ !== undefined) {
          element.style.cursor = this.previousCursor_;
          this.previousCursor_ = undefined;
        }
      }
  }
       
  function handleUpEvent(evt){
      var layers =  mapManager.getLayers();
    var feature = this.feature_;
    
    if(this.feature_.get(ftypes.FTYPE) == ftypes.MARKER){//基地锚点
      if(feature.getId() == "temp_"){
          if(layers.baseSource.getFeaturesAtCoordinate(feature.getGeometry().getCoordinates()).length > 0){//验证基地中心点是否在别的基地中
                feature.getGeometry().setCoordinates(backCoordinate);
                this.coordiante_ = backCoordinate ;
                alert("基地重复!请重新规划");
                this.coordinate_ = null;
                this.feature_ = null;
                return false;
          }
        }else{
            var baseInfo = layers.baseSource.getFeatureById("base_"+feature.get("base_id_"));
            if(baseInfo == null){
                if(layers.baseSource.getFeaturesAtCoordinate(feature.getGeometry().getCoordinates()).length > 0){//验证基地中心点是否在别的基地中
                    feature.getGeometry().setCoordinates(backCoordinate);
                    this.coordiante_ = backCoordinate ;
                    message.error("基地重复!请重新规划");
                    this.coordinate_ = null;
                    this.feature_ = null;
                    return false;
                }
            }else{
                var latlng  = feature.getGeometry().getCoordinates(); 
                var bases = layers.baseSource.getFeaturesAtCoordinate(latlng);
                var base = null;
                if(bases.length>0){
                    base = bases[0];
                }
                var markerId = feature.get("base_id_"); 
                if(base == null || base.get("base_id_") != markerId){
                    feature.getGeometry().setCoordinates(backCoordinate);
                    this.coordiante_ = backCoordinate ;
                    message.error("须放置到基地中!");
                    this.coordinate_ = null;
                    this.feature_ = null;
                    return false;
                }
            }
        }

        if(onMoved){
            onMoved(this.feature_);
        }

    }else if(this.feature_.get(ftypes.FTYPE) == ftypes.DEVICE){
        var geo = feature.getGeometry();
        latlng = geo.getCoordinates();
        bases = layers.baseSource.getFeaturesAtCoordinate(latlng);
        if(bases.length == 0){
            alert("设备须放置到基地中");
            return;
        }
        base = bases[0].get("baseInfo");
        feature.set("baseInfo",base);
    //    $("#device_base_name").val(base.name);
      //  $("#device_base_id").val(base.id);
        //latlng = ol.proj.transform(latlng, 'EPSG:3857', 'EPSG:4326');
        //$("#device_coordinateX").val(latlng[1]);
        //$("#device_coordinateY").val(latlng[0]);
            
    }else if(this.feature_.get(ftypes.FTYPE) == ftypes.FACILITIES){
        
        latlng = feature.getGeometry().getCoordinates();        
        //$("#facilitysForm #latitude").val(latlng[1]);
        //$("#facilitysForm #longitude").val(latlng[0]);
        
    }
    
  this.coordinate_ = null;
  this.feature_ = null;
  return false;
  }
       
       
     

export {moveTarget,Drag,backCoordinate,setOpts};
      